#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os
import pygtk
pygtk.require("2.0")
import gtk
import gtk.gdk
import pango

from time import sleep

from ruler_fun import *
import ruler_menu

reload(sys)
sys.setdefaultencoding('utf8')

class Rule_mouse:
    def __init__(self):
        self.drag   = False
        self.x      = 0
        self.y      = 0

class Rule:
    def on_button_press_event(self, widget, event):
        if event.button == 1:
            self.rule_mouse.drag    = True
            self.rule_mouse.x       = event.x
            self.rule_mouse.y       = event.y
        if event.button == 3:
            self.menu.menu.popup(None, None, None, event.button, event.time)

    def on_button_release_event(self, widget, event):
        if event.button == 1:
            self.rule_mouse.drag = False
            x,y = widget.get_position()
            #if self.pos == 0:
            #    if (self.rule_mouse.x >= self.width-5):
            #        print 'pos = 0,h'
            #        self.win.resize(abs(event.x),self.heigth)
            #        self.draw.set_size_request(abs(event.x),self.heigth)
            #        if event.x < 0:
            #            self.win.move(x+event.x, y)
            #    elif (self.rule_mouse.x <= 5):
            #        print 'pos = 0,v'
            #        self.win.resize(abs(self.width-event.x),self.heigth)
            #        self.draw.set_size_request(abs(self.width-event.x),self.heigth)
            #        if x+event.x <= self.width:
            #            self.win.move(x+event.x, y)
            #        else:
            #            self.win.move(x+self.width, y)
            #else:
            #    if (self.rule_mouse.y >= self.height-5):
            #        print event.y
            #        self.draw.set_size_request(self.width, event.y)
            #        sleep(1)
            #        self.win.resize(self.width, event.y)
            #        self.win.set_size_request(self.width, event.y)

            #        print 'win',self.win.get_size()
            #        print 'draw',self.draw.size_request()

    def on_motion_notify_event(self, widget, event):
        if self.rule_mouse.drag == True:
        #    if not((self.pos == 0 and (self.rule_mouse.x >= self.width-5 or self.rule_mouse.x <= 5)) or \
        #            (self.pos == 1 and (self.rule_mouse.y >= self.height-5 or self.rule_mouse.y <= 5))):
                x,y = widget.get_position()
                widget.move(x+event.x-self.rule_mouse.x, y+event.y-self.rule_mouse.y)

        #if ((self.pos == 0 and (event.x >= self.width-5 or event.x <= 5)) or \
        #    (self.pos == 1 and (event.y >= self.height-5 or event.y <= 5))):
        #    if self.pos == 0:
        #        widget.window.set_cursor(self.cursor_h)
        #    else:
        #        widget.window.set_cursor(self.cursor_v)

        #else:
        #    widget.window.set_cursor(None)

    def draw_cm(self):
        widget_width, widget_heigth = self.draw.size_request()

        if self.pos == 0:
            line_long  = int(widget_heigth/4)
            line_med   = int(widget_heigth/6)
            line_short = int(widget_heigth/12)

            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_width, 0, line_long, 10, 0)
            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_width, widget_heigth-line_long, widget_heigth, 10, 0)

            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_width, 0, line_med, 5, 0)
            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_width, widget_heigth-line_med, widget_heigth, 5, 0)

            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_width, 0, line_short, 1, 0)
            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_width, widget_heigth-line_short, widget_heigth, 1, 0)

            layout = self.draw.create_pango_layout('cm')
            self.draw.window.draw_layout(self.text_gc, 0, line_long+5, layout)
            for i in xrange(1, int(pix_to_mm(widget_width))/10+1):
                layout = self.draw.create_pango_layout('%d' % i)
                self.draw.window.draw_layout(self.text_gc,
                                            int(mm_to_pix((i)*10))-5,
                                            line_long+5,
                                            layout)
        else:
            line_long  = int(widget_width/4)
            line_med   = int(widget_width/6)
            line_short = int(widget_width/12)

            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_heigth, 0, line_long, 10, 1)
            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_heigth, widget_width-line_long, widget_width, 10, 1)

            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_heigth, 0, line_med, 5, 1)
            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_heigth, widget_width-line_med, widget_width, 5, 1)

            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_heigth, 0, line_short, 1, 1)
            draw_graduation_cm(self.draw, self.fg_gc, 0, widget_heigth, widget_width-line_short, widget_width, 1, 1)

            layout = self.draw.create_pango_layout('cm')
            self.draw.window.draw_layout(self.text_gc, line_long+5, 0, layout)
            for i in xrange(1, int(pix_to_mm(widget_heigth))/10+1):
                layout = self.draw.create_pango_layout('%d' % i)
                self.draw.window.draw_layout(self.text_gc,
                                            line_long+10,
                                            int(mm_to_pix((i)*10))-10,
                                            layout)

    def draw_px(self):
        widget_width, widget_heigth = self.draw.size_request()

        if self.pos == 0:
            line_long  = int(widget_heigth/4)
            line_med   = int(widget_heigth/6)
            line_short = int(widget_heigth/12)

            draw_graduation_px(self.draw, self.fg_gc, 0, widget_width, 0, line_long, 100, 0)
            draw_graduation_px(self.draw, self.fg_gc, 0, widget_width, widget_heigth-line_long, widget_heigth, 100, 0)

            draw_graduation_px(self.draw, self.fg_gc, 0, widget_width, 0, line_med, 10, 0)
            draw_graduation_px(self.draw, self.fg_gc, 0, widget_width, widget_heigth-line_med, widget_heigth, 10, 0)

            draw_graduation_px(self.draw, self.fg_gc, 0, widget_width, 0, line_short, 2, 0)
            draw_graduation_px(self.draw, self.fg_gc, 0, widget_width, widget_heigth-line_short, widget_heigth, 2, 0)

            layout = self.draw.create_pango_layout('px')
            self.draw.window.draw_layout(self.text_gc, 0, line_long+5, layout)
            for i in xrange(100, widget_width, 100):
                layout = self.draw.create_pango_layout('%d' % i)
                self.draw.window.draw_layout(self.text_gc,
                                            i-10,
                                            line_long+5,
                                            layout)
        else:
            line_long  = int(widget_width/4)
            line_med   = int(widget_width/6)
            line_short = int(widget_width/12)

            draw_graduation_px(self.draw, self.fg_gc, 0, widget_heigth, 0, line_long, 100, 1)
            draw_graduation_px(self.draw, self.fg_gc, 0, widget_heigth, widget_width-line_long, widget_width, 100, 1)

            draw_graduation_px(self.draw, self.fg_gc, 0, widget_heigth, 0, line_med, 10, 1)
            draw_graduation_px(self.draw, self.fg_gc, 0, widget_heigth, widget_width-line_med, widget_width, 10, 1)

            draw_graduation_px(self.draw, self.fg_gc, 0, widget_heigth, 0, line_short, 2, 1)
            draw_graduation_px(self.draw, self.fg_gc, 0, widget_heigth, widget_width-line_short, widget_width, 2, 1)

            layout = self.draw.create_pango_layout('px')
            self.draw.window.draw_layout(self.text_gc, line_long+5, 0, layout)
            for i in range(100, widget_heigth, 100):
                layout = self.draw.create_pango_layout('%d' % i)
                self.draw.window.draw_layout(self.text_gc,
                                            line_long+2,
                                            i-10,
                                            layout)

    def draw_in(self):
        widget_width, widget_heigth = self.draw.size_request()

        if self.pos == 0:
            line_long  = int(widget_heigth/4)
            line_med   = int(widget_heigth/6)
            line_short = int(widget_heigth/12)

            for i in xrange(0, gtk.gdk.screen_width(), in_to_pix(1)):
                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), 0, line_long, 1, 0)
                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), widget_heigth-line_long, widget_heigth, 1, 0)

                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), 0, line_med, 0.5, 0)
                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), widget_heigth-line_med, widget_heigth, 0.5, 0)

                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), 0, line_short, 0.1, 0)
                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), widget_heigth-line_short, widget_heigth, 0.1, 0)

            layout = self.draw.create_pango_layout('in')
            self.draw.window.draw_layout(self.text_gc, 0, line_long+5, layout)
            for i in xrange(1, int(pix_to_in(widget_width))+1, 1):
                layout = self.draw.create_pango_layout('%d' % i)
                self.draw.window.draw_layout(self.text_gc,
                                            int(in_to_pix(i))-5,
                                            line_long+5,
                                            layout)

        else:
            line_long  = int(widget_width/4)
            line_med   = int(widget_width/6)
            line_short = int(widget_width/12)


            for i in xrange(0, gtk.gdk.screen_height(), in_to_pix(1, 1)):
                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), 0, line_long, 1, 1)
                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), widget_width-line_long, widget_width, 1, 1)

                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), 0, line_med, 0.5, 1)
                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), widget_width-line_med, widget_width, 0.5, 1)

                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), 0, line_short, 0.1, 1)
                draw_graduation_in(self.draw, self.fg_gc, i, in_to_pix(1), widget_width-line_short, widget_width, 0.1, 1)

            layout = self.draw.create_pango_layout('in')
            self.draw.window.draw_layout(self.text_gc, line_long+5, 0, layout)
            for i in xrange(1, int(pix_to_in(widget_heigth))+1, 1):
                layout = self.draw.create_pango_layout('%d' % i)
                self.draw.window.draw_layout(self.text_gc,
                                            line_long+10,
                                            int(in_to_pix(i))-10,
                                            layout)

    def draw_pt(self):
        widget_width, widget_heigth = self.draw.size_request()

        if self.pos == 0:
            line_long  = int(widget_heigth/4)
            line_med   = int(widget_heigth/6)
            line_short = int(widget_heigth/12)
            i=in_to_pix(1, 1)
            for i in xrange(0, (gtk.gdk.screen_width()/in_to_pix(1))+1):
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), 0, line_long, 1, 0)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), widget_heigth-line_long, widget_heigth, 1, 0)

                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), 0, line_med, 0.5, 0)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), widget_heigth-line_med, widget_heigth, 0.5, 0)

                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1)-1, 0, line_short, 1/12.0, 0)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1)-1, widget_heigth-line_short, widget_heigth, 1/12.0, 0)

            layout = self.draw.create_pango_layout('pt')
            self.draw.window.draw_layout(self.text_gc, 0, line_long+5, layout)
            for i in xrange(1, int(pix_to_in(widget_width))+1, 1):
                layout = self.draw.create_pango_layout('%d' % (i*72))
                self.draw.window.draw_layout(self.text_gc,
                                            int(in_to_pix(i))-5,
                                            line_long+5,
                                            layout)

        else:
            line_long  = int(widget_width/4)
            line_med   = int(widget_width/6)
            line_short = int(widget_width/12)

            for i in xrange(0, (gtk.gdk.screen_height()/in_to_pix(1, 1))+1):
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1, 1), 0, line_long, 1, 1)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1, 1), widget_width-line_long, widget_width, 1, 1)

                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), 0, line_med, 0.5, 1)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), widget_width-line_med, widget_width, 0.5, 1)

                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1, 1), 0, line_short, 1/12.0, 1)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1, 1), widget_width-line_short, widget_width, 1/12.0, 1)

            layout = self.draw.create_pango_layout('pt')
            self.draw.window.draw_layout(self.text_gc, line_long+5, 0, layout)
            for i in xrange(1, int(pix_to_in(widget_heigth))+1, 1):
                layout = self.draw.create_pango_layout('%d' % (i*72))
                self.draw.window.draw_layout(self.text_gc,
                                            line_long+2,
                                            int(in_to_pix(i))-10,
                                            layout)

    def draw_pc(self):
        widget_width, widget_heigth = self.draw.size_request()

        if self.pos == 0:
            line_long  = int(widget_heigth/4)
            line_med   = int(widget_heigth/6)
            line_short = int(widget_heigth/12)

            for i in xrange(0, (2*gtk.gdk.screen_height()/in_to_pix(1, 1))+1):
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), 0, line_med, 0.5, 0)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), widget_heigth-line_med, widget_heigth, 0.5, 0)

                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1)-1, 0, line_short, 1/12.0, 0)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1)-1, widget_heigth-line_short, widget_heigth, 1/12.0, 0)

            layout = self.draw.create_pango_layout('pc')
            self.draw.window.draw_layout(self.text_gc, 0, line_long+5, layout)
            for i in xrange(1, int(pix_to_in(widget_width)*2)+1, 1):
                layout = self.draw.create_pango_layout('%d' % (i*3))
                self.draw.window.draw_layout(self.text_gc,
                                            int(in_to_pix(i))/2-5,
                                            line_long+5,
                                            layout)

        else:
            line_long  = int(widget_width/4)
            line_med   = int(widget_width/6)
            line_short = int(widget_width/12)

            for i in xrange(0, (2*gtk.gdk.screen_height()/in_to_pix(1, 1))+1):
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), 0, line_med, 0.5, 1)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1), widget_width-line_med, widget_width, 0.5, 1)

                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1, 1), 0, line_short, 1/12.0, 1)
                draw_graduation_in(self.draw, self.fg_gc, i*in_to_pix(1), in_to_pix(1, 1), widget_width-line_short, widget_width, 1/12.0, 1)

            layout = self.draw.create_pango_layout('pc')
            self.draw.window.draw_layout(self.text_gc, line_long+5, 0, layout)
            for i in xrange(1, int(pix_to_in(widget_heigth)*2)+1, 1):
                layout = self.draw.create_pango_layout('%d' % (i*3))
                self.draw.window.draw_layout(self.text_gc,
                                            line_long+2,
                                            int(in_to_pix(i))/2-10,
                                            layout)

    def draw_expose_event(self, widget, event):
        self.fg_gc.set_rgb_fg_color(self.fg_color)
        self.bg_gc.set_rgb_fg_color(self.bg_color)
        self.text_gc.set_rgb_fg_color(self.text_color)

        self.width, self.heigth = self.win.size_request()

        self.draw.window.draw_rectangle(self.bg_gc, True, 0, 0, self.width, self.height)

        if self.rule_type == 0:
            self.draw_cm()
        elif self.rule_type == 1:
            self.draw_px()
        elif self.rule_type == 2:
            self.draw_in()
        elif self.rule_type == 3:
            self.draw_pt()
        elif self.rule_type == 4:
            self.draw_pc()


    def __init__(self):
        self.win            = gtk.Window()
        self.draw           = gtk.DrawingArea()
        self.cursor_h       = gtk.gdk.Cursor(gtk.gdk.SB_H_DOUBLE_ARROW)
        self.cursor_v       = gtk.gdk.Cursor(gtk.gdk.SB_V_DOUBLE_ARROW)

        self.rule_type      = 4
        self.cursor_value   = False
        self.width          = gtk.gdk.screen_width()#/2
        self.height         = gtk.gdk.screen_height()/15
        self.pos            = 0

        self.rule_mouse     = Rule_mouse()
        self.menu           = ruler_menu.Rule_menu(self)

        self.win.resize(self.width, self.height)
        self.win.set_decorated(False)
        self.win.set_keep_above(True)
        self.win.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DOCK)
        #self.win.set_position(gtk.WIN_POS_CENTER)
        self.win.move(0, 0)
        self.win.add(self.draw)
        self.win.set_events(gtk.gdk.EXPOSURE_MASK       |
                            gtk.gdk.LEAVE_NOTIFY_MASK   |
                            gtk.gdk.BUTTON_PRESS_MASK   |
                            gtk.gdk.BUTTON_RELEASE_MASK |
                            gtk.gdk.POINTER_MOTION_MASK |
                            gtk.gdk.POINTER_MOTION_HINT_MASK)
        #self.draw.realize()
        self.draw.set_size_request(self.width, self.height)

        self.draw.show()
        self.win.show()

        self.fg_gc      = self.draw.style.fg_gc[gtk.STATE_NORMAL]
        self.bg_gc      = self.draw.style.bg_gc[gtk.STATE_NORMAL]
        self.text_gc    = self.draw.style.fg_gc[gtk.STATE_NORMAL]
        self.fg_color   = gtk.gdk.Color(0, 0, 0)
        self.bg_color   = gtk.gdk.Color(231*257, 227*257, 107*257)
        self.text_color = gtk.gdk.Color(0, 0, 0)

        self.draw.connect("expose-event", self.draw_expose_event)
        self.win.connect("button-press-event", self.on_button_press_event)
        self.win.connect("button-release-event", self.on_button_release_event)
        self.win.connect("motion-notify-event", self.on_motion_notify_event)

if __name__ == "__main__":
    w = Rule()
    gtk.main()